package com.example.demo.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//双层参数解析，用于多重sql操作 全称为double deep params parse
public class DDPP {
    public static List<Map<String,String[]>> ddpp(Map<String,String[]> params){
        List<Map<String,String[]>> paramMaps = new ArrayList<>();
        //解决第一层关系
        Map<String,String[]> part1=new HashMap<>();
        for(String key:params.keySet()){
            String temp=key.substring(0,key.indexOf("["));
            String temp2=key.substring(key.indexOf("[")+1,key.lastIndexOf("]"));
            if(part1.containsKey(temp)){
                int i=part1.get(temp).length+1;
                String[] s1=new String[i];
                for (int j = 0; j < part1.get(temp).length; j++) {
                    s1[j] = part1.get(temp)[j];
                }
                s1[i-1]=temp2;
                part1.put(temp,s1);
            }
            else{
                String[] s=new String[1];
                s[0]=temp2;
                part1.put(temp,s);
            }
        }
        for(String key:part1.keySet()){
            Map<String,String[]> part2 = new HashMap<>();
            String[] value=part1.get(key);
            for(String key2:value){
                part2.put(key2,params.get(key+"["+key2+"]"));
            }
            paramMaps.add(part2);
        }
        return paramMaps;
    }

    public static void main(String[] args) {
        Map<String,String[]> params=new HashMap<>();
        String[] s1= {"1"};
        String[] s2= {"wulida"};
        params.put("param1[id]",s1);
        params.put("param1[name]",s2);
        params.put("param2[id]",s1);
        params.put("param2[name]",s2);
        for(Map<String,String[]> s:ddpp(params)){
            for(String key:s.keySet()){
                System.out.println(key+":"+s.get(key)[0]);
            }
        }
    }
}
